package com.qsd.StudentSystem.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsd.StudentSystem.dao.*;
import com.qsd.StudentSystem.service.CourseService;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.po.*;
import com.qsd.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.search.query.QueryManager;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseDao courseDao;
    @Resource
    private ClazzDao clazzDao;
    @Resource
    private PaperDao paperDao;
    @Resource
    private AnswerDao answerDao;
    @Resource
    private GradeDao gradeDao;

    @Override
    public Course search(Integer id) {
        return courseDao.queryCourseById(id);
    }

    @Override
    public ResultStatus join(Integer id, HttpServletRequest request) {
        Student student = (Student) request.getSession().getAttribute("student");
        Clazz clazz = new Clazz(id, student.getUsername());
        //查询是否已经添加
        Integer queryExist = clazzDao.queryExist(clazz);
        if (queryExist != null) {
            return ResultStatus.REQUEST_REPETITION;
        }
        int saveStudent = clazzDao.saveStudent(clazz);
        return saveStudent > 0 ? ResultStatus.SUCCESS : ResultStatus.SYSTEM_ERROR;
    }

    @Override
    public ResultStatus exit(Integer id, HttpServletRequest request) {
        Student student = (Student) request.getSession().getAttribute("student");
        Clazz clazz = new Clazz(id, student.getUsername());
        int removeSudent = clazzDao.removeSudent(clazz);
        return removeSudent > 0 ? ResultStatus.SUCCESS : ResultStatus.SYSTEM_ERROR;
    }

    @Override
    public void queryPaperByCourseId(Integer courseId, Model model) {
        Integer id = courseDao.queryPaperIdById(courseId);
        Paper paper = paperDao.queryById(id);
        List<OneChoice> oneChoices = paperDao.queryOneChoicesByPaperId(id);
        List<MultipleChoice> multipleChoices = paperDao.queryMultipleChoicesByPaperId(id);
        List<Judgment> judgments = paperDao.queryJudgmentsByPaperId(id);
        List<FullInTheBlank> fullInTherBlanks = paperDao.queryFullInTheBlanksByPaperId(id);
        List<Discussion> discussions = paperDao.queryDiscussionsByPaperId(id);
        model.addAttribute("paper", paper);
        model.addAttribute("oneChoices", oneChoices);
        model.addAttribute("multipleChoices", multipleChoices);
        model.addAttribute("judgments", judgments);
        model.addAttribute("fullInTheBlanks", fullInTherBlanks);
        model.addAttribute("discussions", discussions);
    }

    @Override
    public ResultStatus complete(Answer answer, HttpServletRequest request) {
        //那个课程的那张试卷的谁的答案
        Student student = (Student) request.getSession().getAttribute("student");
        String username = student.getUsername();
        answer.setStudentUsername(username);
        int saveAnswer = answerDao.saveAnswer(answer);
        if (saveAnswer > 0) {
            return ResultStatus.SUCCESS;
        }
        return ResultStatus.SYSTEM_ERROR;
    }


    @Override
    public List<Grade> fileList(Grade grade, PageUtil page, Student student) {

        log.info("grade.getCourseName():{}",grade.getCourseName());
        log.info("student.getUsername():{}",student.getUsername());
        IPage<Grade> andPage = gradeDao.findAndPage(new Page<>(page.getPage(), page.getLimit()), grade.getCourseName(), student.getUsername());
        page.setCount((int) andPage.getTotal());
        return andPage.getRecords();
    }
}
